package com.mob.commons.appcollector;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Handler;
import android.text.TextUtils;
import com.mob.commons.g;
import com.mob.tools.MobHandlerThread;
import com.mob.tools.MobLog;
import com.mob.tools.log.NLog;
import com.mob.tools.utils.DeviceHelper;
import com.mob.tools.utils.Hashon;
import com.mob.tools.utils.R;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class PackageCollector
{
  private static PackageCollector b;
  private final String[] a = { "android.intent.action.PACKAGE_ADDED", "android.intent.action.PACKAGE_CHANGED", "android.intent.action.PACKAGE_REMOVED", "android.intent.action.PACKAGE_REPLACED" };
  private Context c;
  private DeviceHelper d;
  private Hashon e;
  private Handler f;

  private PackageCollector(Context paramContext)
  {
    this.c = paramContext.getApplicationContext();
    this.d = DeviceHelper.getInstance(this.c);
    this.e = new Hashon();
  }

  private ArrayList<HashMap<String, String>> a(ArrayList<HashMap<String, String>> paramArrayList1, ArrayList<HashMap<String, String>> paramArrayList2)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator1 = paramArrayList1.iterator();
    label119: 
    while (true)
    {
      HashMap localHashMap;
      if (localIterator1.hasNext())
      {
        localHashMap = (HashMap)localIterator1.next();
        String str = (String)localHashMap.get("pkg");
        if (!TextUtils.isEmpty(str))
        {
          Iterator localIterator2 = paramArrayList2.iterator();
          do
            if (!localIterator2.hasNext())
              break;
          while (!str.equals(((HashMap)localIterator2.next()).get("pkg")));
        }
      }
      else
      {
        for (int i = 1; ; i = 0)
        {
          if (i != 0)
            break label119;
          localArrayList.add(localHashMap);
          break;
          return localArrayList;
        }
      }
    }
  }

  private void a()
  {
    a locala = new a(this);
    locala.start();
    this.f = new Handler(locala.getLooper(), new c(this));
  }

  private void a(long paramLong)
  {
    File localFile = new File(R.getCacheRoot(this.c), "comm/dbs/.nulal");
    if (!localFile.getParentFile().exists())
      localFile.getParentFile().mkdirs();
    try
    {
      DataOutputStream localDataOutputStream = new DataOutputStream(new FileOutputStream(localFile));
      localDataOutputStream.writeLong(paramLong);
      localDataOutputStream.flush();
      localDataOutputStream.close();
      return;
    }
    catch (Throwable localThrowable)
    {
      MobLog.getInstance().d(localThrowable);
    }
  }

  private void a(String paramString, ArrayList<HashMap<String, String>> paramArrayList)
  {
    HashMap localHashMap = new HashMap();
    localHashMap.put("type", paramString);
    localHashMap.put("list", paramArrayList);
    localHashMap.put("datetime", Long.valueOf(com.mob.commons.a.b(this.c)));
    g.a(this.c).a(localHashMap);
  }

  private void a(ArrayList<HashMap<String, String>> paramArrayList)
  {
    File localFile = new File(R.getCacheRoot(this.c), "comm/dbs/.al");
    if (!localFile.getParentFile().exists())
      localFile.getParentFile().mkdirs();
    OutputStreamWriter localOutputStreamWriter;
    try
    {
      localOutputStreamWriter = new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(localFile)), "utf-8");
      Iterator localIterator = paramArrayList.iterator();
      while (localIterator.hasNext())
      {
        HashMap localHashMap = (HashMap)localIterator.next();
        localOutputStreamWriter.append(this.e.fromHashMap(localHashMap)).append('\n');
      }
    }
    catch (Throwable localThrowable)
    {
      MobLog.getInstance().d(localThrowable);
      return;
    }
    localOutputStreamWriter.flush();
    localOutputStreamWriter.close();
  }

  private boolean a(String paramString)
  {
    String[] arrayOfString = this.a;
    int i = arrayOfString.length;
    for (int j = 0; ; j++)
    {
      boolean bool = false;
      if (j < i)
      {
        if (arrayOfString[j].equals(paramString))
          bool = true;
      }
      else
        return bool;
    }
  }

  private void b()
  {
    ArrayList localArrayList1 = c();
    if ((localArrayList1 == null) || (localArrayList1.isEmpty()))
    {
      ArrayList localArrayList2 = this.d.getInstalledApp(false);
      a("APPS_ALL", localArrayList2);
      a(localArrayList2);
      a(com.mob.commons.a.b(this.c) + 1000L * com.mob.commons.a.f(this.c));
      return;
    }
    long l = com.mob.commons.a.b(this.c);
    if (l >= d())
    {
      ArrayList localArrayList3 = this.d.getInstalledApp(false);
      a("APPS_ALL", localArrayList3);
      a(localArrayList3);
      a(l + 1000L * com.mob.commons.a.f(this.c));
      return;
    }
    f();
  }

  private ArrayList<HashMap<String, String>> c()
  {
    File localFile = new File(R.getCacheRoot(this.c), "comm/dbs/.al");
    if (!localFile.getParentFile().exists())
      localFile.getParentFile().mkdirs();
    if (localFile.exists())
      try
      {
        ArrayList localArrayList = new ArrayList();
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(localFile)), "utf-8"));
        for (String str = localBufferedReader.readLine(); str != null; str = localBufferedReader.readLine())
        {
          HashMap localHashMap = this.e.fromJson(str);
          if (localHashMap != null)
            localArrayList.add(localHashMap);
        }
        localBufferedReader.close();
        return localArrayList;
      }
      catch (Throwable localThrowable)
      {
        MobLog.getInstance().d(localThrowable);
      }
    return new ArrayList();
  }

  private long d()
  {
    File localFile = new File(R.getCacheRoot(this.c), "comm/dbs/.nulal");
    if (!localFile.getParentFile().exists())
      localFile.getParentFile().mkdirs();
    if (localFile.exists())
      try
      {
        DataInputStream localDataInputStream = new DataInputStream(new FileInputStream(localFile));
        long l = localDataInputStream.readLong();
        localDataInputStream.close();
        return l;
      }
      catch (Throwable localThrowable)
      {
        MobLog.getInstance().d(localThrowable);
      }
    return 0L;
  }

  private void e()
  {
    IntentFilter localIntentFilter = new IntentFilter();
    for (int i = 0; i < b.a.length; i++)
      localIntentFilter.addAction(b.a[i]);
    localIntentFilter.addDataScheme("package");
    this.c.registerReceiver(new d(this), localIntentFilter);
  }

  private void f()
  {
    ArrayList localArrayList1 = c();
    ArrayList localArrayList2 = this.d.getInstalledApp(false);
    if ((localArrayList1 == null) || (localArrayList1.isEmpty()))
    {
      a("APPS_ALL", localArrayList2);
      a(localArrayList2);
      a(com.mob.commons.a.b(this.c) + 1000L * com.mob.commons.a.f(this.c));
      return;
    }
    ArrayList localArrayList3 = a(localArrayList2, localArrayList1);
    if (!localArrayList3.isEmpty())
      a("APPS_INCR", localArrayList3);
    ArrayList localArrayList4 = a(localArrayList1, localArrayList2);
    if (!localArrayList4.isEmpty())
      a("UNINSTALL", localArrayList4);
    a(localArrayList2);
    a(com.mob.commons.a.b(this.c) + 1000L * com.mob.commons.a.f(this.c));
  }

  public static boolean register(Context paramContext, String paramString)
  {
    try
    {
      startCollector(paramContext);
      return true;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public static void startCollector(Context paramContext)
  {
    try
    {
      if (b == null)
      {
        b = new PackageCollector(paramContext);
        b.a();
      }
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.mob.commons.appcollector.PackageCollector
 * JD-Core Version:    0.6.2
 */